<template>
  <div class="m-point-data">
    <a-spin :spinning="spinning">
      <div class="search-box">
        <div class="left">
          <a-input v-model="pointName" allowClear placeholder="请输入测点">
            <a-icon style="color: #9a9a9a" slot="prefix" type="search" />
          </a-input>
        </div>
        <div class="right">
          <a @click="searchFun()">搜索</a>
        </div>
      </div>
      <div class="big-title">
        <div class="line"></div>
        <div class="title">测点 :</div>
        <div class="point">{{pointName ? pointName :_pointName}}</div>
      </div>
      <div
        class="table-box demo-infinite-container"
        v-infinite-scroll="loadMore"
        :infinite-scroll-disabled="busy"
        :infinite-scroll-distance="50"
      >
        <a-table
          :columns="columns"
          :data-source="listData"
          bordered
          :pagination="false"
        >
          <template slot="value1" slot-scope="text,record">
            <span :style="{'color':record.oneIsWarring == '正常' ? '' : '#FFA201'}">{{ Number(text).toFixed(2)}}</span>
          </template>
          <template slot="value6" slot-scope="text,record">
            <span :style="{'color':record.sumIsWarring == '正常' ? '' : '#FF0000'}">{{ Number(text).toFixed(2)}}</span>
          </template>
        </a-table>
      </div>
    </a-spin>
  </div>
</template>
<script>
import { login,warringByPoint } from "@/request/api";
import infiniteScroll from 'vue-infinite-scroll';
import md5 from "js-md5";
export default {
  props: {},
  directives: {infiniteScroll},
  data() {
    return {
      busy: false,
      _pointName:'',
      proId:'',
      spinning: false,
      pageNum:1,
      pageSize:20,
      ok:false,
      total:1,
      cid: "",
      pointName: "",
      columns: [
        {
          title: "序号",
          dataIndex: "key",
        },
        {
          title: "测点",
          dataIndex: "pointName",
        },
        {
          title: "本次值",
          dataIndex: "value1",
          scopedSlots: { customRender: "value1" },
        },
        {
          title: "累计值",
          dataIndex: "value6",
          scopedSlots: { customRender: "value6" },
        },
        {
          title: "时间",
          dataIndex: "date",
        },
      ],
      listData: [],
    };
  },
  components: {},
  async created() {
    this.proId = this.$route.query.proId;
    this._pointName = this.$route.query.pointName
    this.spinning = true;
    let { data } = await login({
      userName: "18688793445",
      passWord: md5("guangliantest"),
    });
    console.log(data);
    if (data.code === 200) {
      localStorage.setItem("token", data.result.token);
      localStorage.setItem("userInfo", JSON.stringify(data.result.userInfo));
      this.$store.commit("setUserInfo", data.result.userInfo);
      this.ok = true;
      this.warringByPoint(); //数据列表
      this.spinning = false;
    }
  },
  mounted() {},
  methods: {
    searchFun(){
      this.pageNum = 1;
      this.listData = [];
      this.warringByPoint(); //数据列表
    },
    loadMore(){
      console.log('进入加载事件')
      this.busy = true
      if(this.ok){
        if(this.listData.length<this.total){
          this.warringByPoint();
        }
      }
    },
    //列表
    async warringByPoint() {
      this.spinning = true;
      let { data } = await warringByPoint({
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        proId:this.proId,
        pointName:this.pointName ? this.pointName : this._pointName,
      });
      if (data.code === 200) {
        this.pageNum ++
        this.total = data.result.total
        this.listData = [...this.listData , ...data.result.list];
        this.listData.forEach((item, index) => {
          item.key = index + 1 
        });
        this.busy = false;
        this.spinning = false;
      }
    },
  },
};
</script>
<style lang="less" scoped>
.search-box {
  margin: 9px 10px 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .left {
    flex-grow: 1;
    /deep/ .ant-input {
      background: #ffffff;
      box-shadow: 0.5px 1px 12px 0px rgba(106, 121, 176, 0.16);
      border-radius: 6px;
      border: 0;
    }
  }
  .right {
    margin-left: 15px;
    a {
      font-size: 15px;
    }
  }
}
.big-title {
  margin: 20px 15px 0;
  display: flex;
  align-items: center;
  font-size: 15px;
  font-weight: bold;
  padding: 10px 0;
  .line {
    width: 4px;
    background: #1890ff;
    height: 12px;
  }
  .title {
    margin-left: 10px;
  }
  .point {
    color: #1890ff;
    margin-left: 5px;
  }
}
.table-box {
  margin: 20px 10px 50px;
  /deep/ th {
    padding: 5px;
    text-align: center;
    font-size: 13px;
    background: #fff !important;
  }
  /deep/ td {
    padding: 5px;
    text-align: center;
    height: 60px;
  }
}
.m-point-data {
  min-height: 100vh;
  background: #fefeff;
  font-size: 13px;
}
</style>